﻿using BuyWithoutLoseMoney;
using BuyWithoutLoseMoney.Core.Models;
using BuyWithoutLoseMoney.Core.Repositories;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;

namespace BuyWithoutLose.Areas.Admin.Controllers
{
    /// <summary>
    /// 关于我们
    /// </summary>
    [Area("admin")]
    public class AboutController : BaseAdminController
    {
        private readonly IRepository<AboutUS, int> _abourusrepository;
        public AboutController(IRepository<AboutUS,int> abourusrepository, BuyWithoutLoseMoneyDBContext db) : base(db)
        {
            _abourusrepository = abourusrepository;
        }
        #region 关于我们的列表，查询，新建，修改，删除
        /// <summary>
        /// 关于我们列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Index()
        {
            var aboutus=await _abourusrepository.GetAllListAsync();
            return View(aboutus);
        }
        
        /// <summary>
        /// 查询title
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Index(int page, int limit, string title)
        {
            var aboutus = await _abourusrepository.GetAllListAsync();
            var query = aboutus.Where(x => true);
            if(!string.IsNullOrEmpty(title))
            {
                query=query.Where(x=> x.Title == title);    
            }
            var count=query.Count();
            var list = query.OrderBy(x => x.Id).Skip((page - 1) * limit).Take(limit).ToList()
              .Select(x => new
              {
                 id=x.Id,
                 title=x.Title,
                 phone=x.Phone,
                 email=x.EMail,
                 blogroll=x.BloGroll

              }); 
            return Json(new { Code = 0, Msg = "", Data = list, Count = count });
        }
        /// <summary>
        /// 添加更多信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Create()
        {
            var aboutus = await _abourusrepository.GetAllListAsync();
            ViewBag.Aboutus=aboutus;
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Create(AboutUS aboutUS)
        {
            if (ModelState.IsValid)
            {
                await _abourusrepository.InsertAsync(aboutUS);
                return Json(new { isSuccess = true, msg = "添加成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "添加失败，请重试！" });
        }
        /// <summary>
        /// 修改我们的信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Edit(int id)
        {
            var aboutus=await _abourusrepository.GetAllListAsync();
            var us=aboutus.FirstOrDefault(x=>x.Id==id);
            return View(us);
        }
        [HttpPost]
        public async Task<IActionResult> Edit(AboutUS aboutUS)
        {
            if (ModelState.IsValid)
            {
                await _abourusrepository.UpdateAsync(aboutUS);
                return Json(new { isSuccess = true, msg = "修改成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "修改失败，请重试！" });
        }
       /// <summary>
       /// 删除
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
        public async Task<IActionResult> Del(int id)
        {
            AboutUS p = new AboutUS() { Id = id };
            if (ModelState.IsValid)
            {
                await _abourusrepository.DeleteAsync(p);
                return Json(new { isSuccess = true, msg = "删除成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "删除失败，请重试！" });
        }
        #endregion
    }
}
